<?php
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_user.php";
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_redirect.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_security.php';

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of api_security
 *
 * @author Thien
 */
class api_user {
    
    public static function get_all_user() {
        $user_access = new dao_user();
        $useres = $user_access->get_all();
        return $useres;
    }
    
    public static function get_all_user_categories() {
        $user_category_access = new dao_user_category();
        return $user_category_access->get_all();
    }
    
    public static function delete_user($id) {
        $user_access = new dao_user();
        $user_access->delete($id);
        return TRUE;
    }

    public static function delete_user_category($id) {
        $user_category_access = new dao_user_category();
        $user_category_access->delete($id);
        return TRUE;
    }
    
    public static function save_user($user_id, $email, $first_name,
            $last_name, $address, $phone) {
        $user_access = new dao_user();
        if ($user_id == 0) {
            $password = "123456";
        } else {
            $user = self::get_user_by_id($user_id);
            $password = $user['password'];
        }
        $user_access->save($user_id,$email,$password,2,$first_name,$last_name,$address,$phone);
        return TRUE;
    }
    
    public static function add_user_category($cate_user_name, $parent_cat_user) {
        $user_category_access = new dao_user_category();
        $user_category_access->save(0,$cate_user_name,$parent_cat_user);
        return TRUE;
    }
    
    public static function get_user_by_id($id) {
        $user_access = new dao_user();
        return $user_access->get_by_id($id);
    }
    
    public static function get_user_category_by_id($id) {
        $user_category_access = new dao_user_category();
        return $user_category_access->get_by_id($id);
    }
    
    public static function count_user_by_category_id($category_id) {
        $user_access = new dao_user();
        return $user_access->count_by_category_id($category_id);
    }
    
    public static function get_all_user_by_category_id($category_id) {
        $user_access = new dao_user();
        return $user_access->get_all_by_category_id($category_id);
    }
    
    public static function get_all_latest_user($num_user) {
        $user_access = new dao_user();
        return $user_access->get_all_latest($num_user);
    }
    
    public static function email_exists($email) {
        
        $dao_user = new dao_user();
        $list = $dao_user->get_by_email($email);
        if (!empty($list) && count($list) > 0) {
            
            return TRUE;
        }
        return FALSE;
    }
    
    public static function get_user_by_email($email) {
        $dao_user = new dao_user();
        $user = $dao_user->get_by_email($email);
        return $user;
    }
    
    public static function validate_user_fields($user_id, $email_save, $first_name_save, $last_name_save, $address_save, $phone_save) {
        $error_show = "";
        if (empty($email_save)) {
            $error_show .= "Bạn quên nhập email.";
        } else if (!filter_var($email_save, FILTER_VALIDATE_EMAIL)) {
            $error_show .= "Email không hợp lệ.";
        } else if (self::email_exists($email_save) && $user_id == 0) {
            $error_show .= "Email đã tồn tại. Vui lòng chọn email khác.";
        } else if (empty($first_name_save)) {
            $error_show .= "Bạn quên nhập họ.";
        } else if (empty($last_name_save)) {
            $error_show .= "Bạn quên nhập tên.";
        } else if (empty($address_save)) {
            $error_show .= "Bạn quên nhập địa chỉ.";
        } else if (empty($phone_save)) {
            $error_show .= "Bạn quên nhập số điện thoại.";
        } else if (!is_numeric($phone_save)) {
            $error_show .= "Số điện thoại chỉ được nhập số.";
        }
        return $error_show;
    }
}
